package com.iris.live.services.report.sources;

import com.iris.live.services.data.models.Lsh经销商表Model;
import com.iris.live.services.data.models.用户Model;
import com.iris.live.services.data.repositories.Lsh经销商表Repository;
import com.iris.live.services.data.repositories.ReportRepository;
import com.iris.live.services.data.repositories.用户Repository;
import com.iris.live.services.report.IExcelReportSource;
import com.iris.live.services.report.Sheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by lusong on 2016/12/13.
 */
public abstract class AbstractReportSourceService implements IExcelReportSource {

    private Logger logger = LoggerFactory.getLogger(AbstractReportSourceService.class);

    @Autowired
    private 用户Repository repository用户;

    @Autowired
    private Lsh经销商表Repository lsh经销商表Repository;

    @Autowired
    private ReportRepository reportRepository;

    @Override
    public Sheet[] getSheets(Map<String, Object> context) {
        List list = new ArrayList();
        List<Sheet> sheets = new ArrayList<>();
        try {
            用户Model model = repository用户.findByCode(String.valueOf(context.get("username")));
            if (model != null) {
                if ("集团".equals(model.get角色())) {
                    list = reportRepository.getNextAboutLsh();
                } else if ("大区".equals(model.get角色())) {
                    list = reportRepository.getNextAboutDistrict(model.get许可());
                } else if ("小区".equals(model.get角色())) {
                    list = reportRepository.getNextAboutVillage(model.get许可());
                } else if ("CTA".equals(model.get角色())) {
                    list = reportRepository.getNextAboutCTA(model.get许可());
                } else {
                    Lsh经销商表Model lsh经销商表Model = lsh经销商表Repository.getLshDistributorByPermit(model.get许可());
                    sheets.add(new Sheet(model.get许可(), lsh经销商表Model.get经销商名(), true));
                }
            } else {
                Lsh经销商表Model lsh经销商表Model = lsh经销商表Repository.getLshDistributorByPermit(String.valueOf(context.get("permit")));
                sheets.add(new Sheet("one," + lsh经销商表Model.getBmbs(), lsh经销商表Model.getSalesCode(), true));
            }

            if (list != null && list.size() > 0) {
                if ("集团".equals(model.get角色())) {
                    sheets.add(new Sheet("LSH", "LSH", true));
                } else {
                    sheets.add(new Sheet(model.get许可(), model.get许可(), true));
                }
                for (int i = 1; i < list.size() + 1; i++) {
                    sheets.add(new Sheet(((Map) list.get(i - 1)).get("result").toString(), ((Map) list.get(i - 1)).get("result").toString(), true));
                }
            }
        } catch (Exception e) {
            logger.error("获取区域类型异常", e);
        }

        return sheets.toArray(new Sheet[sheets.size()]);
    }
}
